//
//  SettingViewController.m
//  sy_pad_l
//
//  Created by Chiu Chi Ho on 4/2/13.
//  Copyright (c) 2013 Chiu Chi Ho. All rights reserved.
//

#import "SettingViewController.h"
#import "PreferenceManager.h"
#import "DigitAlertPrompt.h"

@interface SettingViewController ()

@end

@implementation SettingViewController
@synthesize tableView = _tableView;


- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
		self.title = NSLocalizedString(@"Setting", @"Setting");
		self.tabBarItem.image = [UIImage imageNamed:@"Second"];
		
		self.navigationItem.title = @"Setting";
    }
    return self;
}

- (void)viewDidUnload
{
	[[NSNotificationCenter defaultCenter] removeObserver:self];
	
	[super viewDidUnload];
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
	
	[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(didReceiveNotification:) name:@"GroupIdDidChange" object:nil];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - NSNotificationCenter
- (void)didReceiveNotification:(NSNotification *)notification
{
	if([[notification name] isEqualToString:@"GroupIdDidChange"])
	{
		[self.tableView reloadData];
	}
}

#pragma mark - UITableViewDataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
	return 2;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
	if(section == 0)
		return 2;
	else if(section == 1)
		return 3;
	
	return 0;
}

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section;
{
	if(section == 0)
		return NSLocalizedString(@"Setting", @"");
	else if(section == 1)
		return NSLocalizedString(@"General", @"");
	
	return @"";
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *SubtitleCellId = @"SubtitleCell";
	static NSString *DefaultCellId = @"DefaultCell";
	
	NSString *reuseIdentifer = nil;
	
	if(indexPath.section == 0)
	{
		reuseIdentifer = SubtitleCellId;
	}
	else if(indexPath.section == 1)
	{
		reuseIdentifer = DefaultCellId;
	}
	

	UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:reuseIdentifer];
	if (cell == nil) {
		cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:reuseIdentifer] autorelease];
	}

	//Reset everythings
	cell.detailTextLabel.text = @"";
	
	if(indexPath.section == 0)
	{
		if(indexPath.row == 0)
		{
			cell.textLabel.text = NSLocalizedString(@"Notify when event begins", @"");
			
			BOOL bIsOn = [[PreferenceManager sharedManager] loadNotificationSetting];
			if(bIsOn)
				cell.detailTextLabel.text = NSLocalizedString(@"On", @"");
			else
				cell.detailTextLabel.text = NSLocalizedString(@"Off", @"");
			
		}
		else if(indexPath.row == 1)
		{
			cell.textLabel.text = NSLocalizedString(@"3rd digit", @"");
			cell.detailTextLabel.text = [[PreferenceManager sharedManager] loadGroupId];
		}
	}
	else if(indexPath.section == 1)
	{
		if(indexPath.row == 0)
			cell.textLabel.text = NSLocalizedString(@"Report bugs", @"");
		else if(indexPath.row == 1)
			cell.textLabel.text = NSLocalizedString(@"Contact us", @"");
		else if(indexPath.row == 2)
			cell.textLabel.text = NSLocalizedString(@"Like us", @"");
	}
	
	return cell;
}

#pragma mark - UITableViewDelegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
	if(indexPath.section == 0)
	{
		if(indexPath.row == 0)	//Notification
		{
			PreferenceManager *mgr = [PreferenceManager sharedManager];
			BOOL bIsOn = [mgr loadNotificationSetting];

			[mgr saveNotificationSetting:!bIsOn];	//Save the opposite one
			
			[self.tableView reloadData];
		}
		else if(indexPath.row == 1)	//Digit
		{
			DigitAlertPrompt *prompt = [[DigitAlertPrompt alloc] initWithForceInput:NO];
			[prompt show];
			[prompt release];
		}
	}
}

@end
